import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
print('all libs loaded')
# start looking at the data
gas = pd.read_csv('gas_prices.csv')
gas
x = [0,1,2,3,4]
y = [0,2,4,6,8]
# Resize your Graph (dpi specifies pixels per inch. When saving probably should use 300 if possible)
plt.figure(figsize=(8,5), dpi=100)
# Line 1
# Keyword Argument Notation
#plt.plot(x,y, label='2x', color='red', linewidth=2, marker='.', linestyle='--',
#markersize=10, markeredgecolor='blue')
# Shorthand notation
# fmt = '[color][marker][line]'
plt.plot(x,y, 'b^--', label='2x')
## Line 2
# select interval we want to plot points at
x2 = np.arange(0,4.5,0.5)
# Plot part of the graph as line
plt.plot(x2[:6], x2[:6]**2, 'r', label='X^2')
# Plot remainder of graph as a dot
plt.plot(x2[5:], x2[5:]**2, 'r--')
# Add a title (specify font parameters with fontdict)
plt.title('Our First Graph!', fontdict={'fontname': 'Comic Sans MS', 'fontsize': 20})
# X and Y labels
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# X, Y axis Tickmarks (scale of your graph)
plt.xticks([0,1,2,3,4,])
#plt.yticks([0,2,4,6,8,10])
# Add a legend
plt.legend()
# Save figure (dpi 300 is good when saving so graph has high resolution)
plt.savefig('mygraph.png', dpi=300)
# Show plot
plt.show()
labels = ['A', 'B', 'C']
values = [1,4,2]
plt.figure(figsize=(5,3), dpi=100)
bars = plt.bar(labels, values)
patterns = ['/', 'O', '*']
for bar in bars:
bar.set_hatch(patterns.pop(0))
plt.savefig('barchart.png', dpi=300)
plt.show()
#set the figure size
plt.figure(figsize=(9,7))
# Add a title (specify font parameters with fontdict)
plt.title('Gas Over Time', fontdict={'fontname': 'Times', 'fontsize': 20})
#do simple ploting on the data
#use short hand for style
# plt.plot(gas.Year, gas.USA,'b.-', label='USA')
# plt.plot(gas.Year, gas.Canada,'r.-', label ='Canada')
# plt.plot(gas.Year, gas['South Korea'],'g.-', label = 'SK')
# plt.plot(gas.Year, gas['Australia'],'y.-', label = 'SK')
#plot specific members of the data chart
# countries_to_look_at = ['Australia', 'USA', 'Canada', 'South Korea']
# for country in gas:
# if country in countries_to_look_at:
# plt.plot(gas.Year, gas[country], marker='.', label =country)
#plot the whole data chart
for country in gas:
if country != 'Year':
plt.plot(gas.Year, gas[country], marker='.', label =country)
#ticks
year_ticks = (gas.Year[::3])
plt.xticks(year_ticks.tolist()+[2011])
# X and Y labels
plt.xlabel('Year')
plt.ylabel('US Dollars')
#save plot
#plt.savefig('Gas Price Fig', dpi=300)
#legend
plt.legend()
plt.show()
fifa = pd.read_csv('fifa_data.csv')
fifa.head(5)
bins = [40,50,60,70,80,90,100]
plt.figure(figsize=(8,5))
plt.hist(fifa.Overall, bins=bins, color='#abcdef')
plt.xticks(bins)
plt.ylabel('Number of Players')
plt.xlabel('Skill Level')
plt.title('Distribution of Player Skills in FIFA 2018')
#to save data
# plt.savefig('histogram.png', dpi=300)
plt.show()
#bring in data from certain rows
left = fifa.loc[fifa['Preferred Foot'] == 'Left'].count()[0]
right = fifa.loc[fifa['Preferred Foot'] == 'Right'].count()[0]
#set up chart values
plt.figure(figsize=(8,5))
labels = ['Left', 'Right']
colors = ['#abcdef', '#aabbcc']
#build out the chart
plt.pie([left, right], labels = labels, colors=colors, autopct='%.2f %%')
plt.title('Foot Preference of FIFA Players')
#show chart
plt.show()
#get player weights
fifa.Weight = [int(x.strip('lbs')) if type(x)==str else x for x in fifa.Weight]
#break down the weight classes
light = fifa.loc[fifa.Weight < 125].count()[0]
light_medium = fifa[(fifa.Weight >= 125) & (fifa.Weight < 150)].count()[0]
medium = fifa[(fifa.Weight >= 150) & (fifa.Weight < 175)].count()[0]
medium_heavy = fifa[(fifa.Weight >= 175) & (fifa.Weight < 200)].count()[0]
heavy = fifa[fifa.Weight >= 200].count()[0]
#prepare data for charting
weights = [light,light_medium, medium, medium_heavy, heavy]
label = ['under 125', '125-150', '150-175', '175-200', 'over 200']
explode = (.4,.2,0,0,.4)
#set chart style
plt.style.use('ggplot')
plt.figure(figsize=(8,5), dpi=100)
#show the charting data
plt.pie(weights, labels=label, pctdistance=0.8, autopct='%.1f %%', explode =explode)
plt.title('Weight Distribution of FIFA Players')
plt.show()
#lets take a look at the overall scores of the differnt clubs
barcelona = fifa.loc[fifa.Club == "FC Barcelona"]['Overall']
madrid = fifa.loc[fifa.Club == "Real Madrid"]['Overall']
revs = fifa.loc[fifa.Club == "New England Revolution"]['Overall']
#set up chart
plt.figure(figsize=(7,8), dpi=100)
plt.style.use('default')
labels=['FC Barcelona','Real Madrid', 'NE Revolution']
#data , Labels, Patch Artist Tools Active, Median Line Size
bp = plt.boxplot([barcelona, madrid, revs], labels = labels,
patch_artist=True, medianprops={'linewidth': 2})
plt.title('Professional Soccer Team Comparison')
plt.ylabel('FIFA Overall Rating')
#set the color for each box
for box in bp['boxes']:
# change outline color
box.set(color='#4286f4', linewidth=2)
# change fill color
box.set(facecolor = '#e0e0e0' )
# change hatch
box.set(hatch = '/')
plt.show()